package com.ruoyi.student.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.student.mapper.PhdAdmissionInfoMapper;
import com.ruoyi.student.domain.PhdAdmissionInfo;
import com.ruoyi.student.service.IPhdAdmissionInfoService;

import static com.ruoyi.common.core.domain.AjaxResult.error;

/**
 * 博士录取信息Service业务层处理
 * 
 * @author ruoyi-mate
 * @date 2025-09-10
 */
@Service
public class PhdAdmissionInfoServiceImpl implements IPhdAdmissionInfoService 
{
    @Autowired
    private PhdAdmissionInfoMapper phdAdmissionInfoMapper;

    /**
     * 查询博士录取信息
     * 
     * @param id 博士录取信息主键
     * @return 博士录取信息
     */
    @Override
    public PhdAdmissionInfo selectPhdAdmissionInfoById(String id)
    {
        return phdAdmissionInfoMapper.selectPhdAdmissionInfoById(id);
    }

    /**
     * 查询博士录取信息列表
     * 
     * @param phdAdmissionInfo 博士录取信息
     * @return 博士录取信息
     */
    @Override
    public List<PhdAdmissionInfo> selectPhdAdmissionInfoList(PhdAdmissionInfo phdAdmissionInfo)
    {
        return phdAdmissionInfoMapper.selectPhdAdmissionInfoList(phdAdmissionInfo);
    }

    /**
     * 新增博士录取信息
     * 
     * @param phdAdmissionInfo 博士录取信息
     * @return 结果
     */
    @Override
    public int insertPhdAdmissionInfo(PhdAdmissionInfo phdAdmissionInfo)
    {
        return phdAdmissionInfoMapper.insertPhdAdmissionInfo(phdAdmissionInfo);
    }

    /**
     * 修改博士录取信息
     * 
     * @param phdAdmissionInfo 博士录取信息
     * @return 结果
     */
    @Override
    public int updatePhdAdmissionInfo(PhdAdmissionInfo phdAdmissionInfo)
    {
        return phdAdmissionInfoMapper.updatePhdAdmissionInfo(phdAdmissionInfo);
    }

    /**
     * 批量删除博士录取信息
     * 
     * @param ids 需要删除的博士录取信息主键
     * @return 结果
     */
    @Override
    public int deletePhdAdmissionInfoByIds(String[] ids)
    {
        return phdAdmissionInfoMapper.deletePhdAdmissionInfoByIds(ids);
    }

    /**
     * 删除博士录取信息信息
     * 
     * @param id 博士录取信息主键
     * @return 结果
     */
    @Override
    public int deletePhdAdmissionInfoById(String id)
    {
        return phdAdmissionInfoMapper.deletePhdAdmissionInfoById(id);
    }

    @Override
    public String importUser(List<PhdAdmissionInfo> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PhdAdmissionInfo user : userList)
        {
            try
            {
                user.setCreateBy(operName);
                this.insertPhdAdmissionInfo(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " +" 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<PhdAdmissionInfo> selectBatchByStudentIds(PhdAdmissionInfo query) {
        return phdAdmissionInfoMapper.selectBatchByStudentIds(query);
    }

    @Override
    public boolean checkStudentIdExists(String studentId) {
        return phdAdmissionInfoMapper.checkStudentIdExists(studentId) > 0;
    }


}
